package model

// by daoyi
import (
	"encoding/json"
	"fmt"
	"reflect"
	"simai_oa/tool"
	"strings"
	"time"

	"gorm.io/gorm"
)

type UserBasic struct {
	gorm.Model
	Name          string
	Password      string
	Salt          string
	Phone         string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email         string `valid:"email"`
	Identity      string
	ClientIP      string
	ClientPort    string
	LoginTime     time.Time `gorm:"column:login_time" json:"login_time"`
	HeartbeatTime time.Time `gorm:"column:heartbeat_time" json:"heartbeat_time"`
	LoginOutTime  time.Time `gorm:"column:login_out_time" json:"login_out_time"`
	IsLogout      bool
	DeviceInfo    string
	CreditCard    []CreditCard
}
type CreditCard struct {
	gorm.Model
	Number      string
	UserBasicID uint
}

// 检索用户列表并预加载信用卡
// func GetAll() []*UserBasic {
func GetAll() interface{} {
	users := make([]*UserBasic, 0)
	tool.Gdb.Preload("CreditCard").Find(&users)
	// for _, v := range users {
	// 	fmt.Println(v)
	// }
	buf, _ := json.Marshal(users)
	// buf1 := []interface{}{}
	buf1 := make([]interface{}, 0)

	sliceMap := make(map[string][]int)
	sliceMap["中国"] = make([]int, 0)
	sliceMap["中国"] = append(sliceMap["中国"], 89)
	sliceMap["中国"][0] = 90
	fmt.Println(sliceMap)
	// fmt.Println(string(buf))
	json.Unmarshal(buf, &buf1)
	// fmt.Println(&buf1)
	for k, v := range buf1 {
		v.(map[string]interface{})["test"] = "ok"
		formatTimeStr := v.(map[string]interface{})["login_time"]
		fmt.Printf("%#v\n", formatTimeStr)
		fmt.Printf("%#v\n", formatTimeStr.(string))
		//"2012-12-12T13:14:17+0800"

		//T to Str
		time_str := formatTimeStr.(string)[:19]
		fmt.Println(time_str)
		time_str = strings.Replace(time_str, "T", " ", -1)
		fmt.Println(time_str) // "2012-12-12 13:14:17"

		v.(map[string]interface{})["login_time"] = time_str
		fmt.Println(reflect.TypeOf(buf1))
		fmt.Printf("%v\n%v", k, v)
	}

	var scoreMap = make(map[string]int, 2)
	scoreMap["name"] = 1
	scoreMap["age"] = 12
	scoreMap["addr"] = 92
	fmt.Println(scoreMap)

	buf1 = append(buf1, []map[string]interface{}{
		{"name": "金沙江", "age": 12},
		{"name": "金沙江1", "age": 122}},
	)

	s := "first"
	rest := []interface{}{"second", 3}

	all := append([]interface{}{s}, rest...)
	fmt.Println(all)

	a := []int{1, 2, 3, 4, 5}
	b := make([]int, 4, 5)
	b = append(b, []int{1, 2, 3, 4, 5, 6}...)
	// copy(b,a)
	fmt.Println(a)
	fmt.Println(b)

	// j := &buf1
	// fmt.Println(*j)
	// fmt.Println(j)
	// fmt.Printf("%v %p\n",j, j)

	return &buf1
}

func GetUserList() []*UserBasic {
	data := make([]*UserBasic, 10)
	tool.Gdb.Find(&data)
	for _, v := range data {
		fmt.Println(v)
	}
	return data
}

func FindUserByName(name string) UserBasic {
	user := UserBasic{}
	tool.Gdb.Where("name = ?", name).First(&user)
	return user
}

func FindUserByPhone(phone string) UserBasic {
	user := UserBasic{}
	tool.Gdb.Where("phone = ?", phone).First(&user)
	return user
}

func FindUserByEmail(email string) UserBasic {
	user := UserBasic{}
	tool.Gdb.Where("email = ?", email).First(&user)
	return user
}

func FindUserByNamePwd(name string, password string) UserBasic {
	user := UserBasic{}
	tool.Gdb.Where("name = ? and password = ?", name, password).First(&user)
	return user
}

func CreateUser(user UserBasic) *gorm.DB {
	return tool.Gdb.Create(&user)
}

func UpdateUser(user UserBasic) *gorm.DB {
	return tool.Gdb.Model(&user).Updates(UserBasic{Name: user.Name, Password: user.Password, Phone: user.Phone, Email: user.Email})
}

func DeleteUser(user UserBasic) *gorm.DB {
	return tool.Gdb.Delete(&user)
}
